package com.tledu.controller;



import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.github.pagehelper.PageInfo;

import com.tledu.commont.ResultVo;
import com.tledu.pojo.Section;
import com.tledu.pojo.Subject;
import com.tledu.service.SubjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


/**
 * @Author: wzw
 * @Date: 2022/5/24
 */
@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    /**
     * 添加专题
     * @param subject
     * @return
     */
    @PutMapping("/addSubject")
    public ResultVo addSubject(@RequestBody Subject subject) {
        boolean result = subjectService.save(subject);
        if (result) {
            return new ResultVo(200, "添加成功", null);
        }
        return new ResultVo(401, "添加失败", null);
    }

    /**
     * 删除专题
     * @param subject_id
     * @return
     */
    @DeleteMapping("/deleteSubject/{subject_id}")
    public ResultVo deleteSubject(@PathVariable("subject_id") Integer subject_id){
        int result=subjectService.deleteById(subject_id);
        if (result > 0) {
            return new ResultVo(200, "删除成功", null);
        }
        return new ResultVo(401, "删除失败", null);
    }



    /**
     * 分页查询
     * @param current 当前页  第零页和第一页的数据是一样
     * @param size 每一页的数据条数
     * @author wzw
     * @return Result对象
     */
    @GetMapping(value = "/selectPage/{current}/{size}")
    public ResultVo selectPage(@PathVariable("current") Integer current, @PathVariable("size") Integer size,
                                 @RequestParam(required = false,value = "subject_title") String subject_title ) {
        PageInfo<Subject> page = subjectService.selectPage(current,size,subject_title);
        if (Objects.nonNull(page)) {
            return new ResultVo(200, "查询成功", page);
        }
        return new ResultVo(401, "查询失败", page);
    }

    /**
     * 查询专题
     * @return
     */
    @GetMapping(value = "/selectSubject")
    public ResultVo selectSubject() {
        List<Subject> subjectList = subjectService.list();
        return new ResultVo(200, "",subjectList);
    }


    /**
     * 更新专题
     * @param subject 章节对象
     * @return ResultVo
     */
    @PutMapping("/update")
    public ResultVo updateSection(@RequestBody Subject subject) {
        System.out.println(subject.getSubject_id());
        boolean result = subjectService.updateById(subject);
        if (result) {
            return new ResultVo(200, "修改成功", null);
        }
        return new ResultVo(401, "修改失败", null);
    }

}
